/*
 * @Author: HeAo
 * @Date: 2021-11-03 14:34:20
 * @LastEditTime: 2021-11-03 15:00:04
 * @LastEditors: HeAo
 * @Description:
 * @FilePath: \web2103-2105\web2105\4-1-Nodejs\day04\app01\index.js
 * 别乱动！
 */
const express = require('express');
// 导入中间件
const bodyParser = require('body-parser');

// 1.创建服务实例
const app = express()

// 使用中间件
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

// 封装响应结果
class Response {
  constructor(message, data) {
    this.message = message
    this.data = data
    this.status = 20000
    this.timestamp = new Date().getTime()
  }
}

app.get('/user/findAll', (req, res) => {
  const users = [
    { id: 1, name: 'terry', age: 12 },
    { id: 2, name: 'larry', age: 12 },
    { id: 3, name: 'tom', age: 12 },
  ]
  res.send(new Response('查询成功', users))  // res.write res.end 这两个方法的集合
})

app.delete('/user/deleteById', (req, res) => {
  console.log(req.query.id);
  res.send(new Response('删除成功', null))
})

app.post('/user/saveOrUpdate', (req, res) => {
  console.log(req.body);
  if (req.body.id) {
    res.send(new Response('修改成功', null))
  } else {
    res.send(new Response('保存成功', null))
  }
})

// 2.监听端口
app.listen(3000, () => {
  console.log('3000 server is running');
})


